﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ext.Net;
using FSS.Core.Controls;
using FSS.BLL.Entities;
using FSS.BLL.Services;
using FSS.BLL;
using FSS.Core.Util;

namespace SalesManagement.Controls.List
{
    public partial class ProductPriceForm : BaseControl
    {
        public event EventHandler SaveClick;
        private void OnSaveClick()
        {
            if (SaveClick != null)
            {
                SaveClick(this, EventArgs.Empty);
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        public void Show(int id)
        {
            FormPrice.Reset();
            dfApplyDate.SelectedDate = DateTime.Now;
            txtProductID.Text = id.ToString();            
            this.winDetails.Show();
        }
        public void ShowEdit(int id)
        {
            txtPriceId.Text = id.ToString();
            ProductPriceEntity priceE = new ProductPriceEntity();
            ProductPriceService priceS = ProductPriceService.GetInstance();
            priceE.ProductPriceId = id;
            priceE = priceS.LoadByPrimaryKey(priceE);
            if (!priceE.Equals(null))
            {
                dfApplyDate.SelectedDate = priceE.ApplyDate;
                txtPrice.Text =Format.ConvertToInt(priceE.Price).ToString(); 
            }
            txtProductID.Text = priceE.ProductId.ToString();
            this.winDetails.Show();
        }
        protected void btnSave_Click(object sender, DirectEventArgs e)
        {
            InsertData();
            OnSaveClick();
            this.winDetails.Hide();
        }
        private bool CheckData()
        {
            return true;
        }
        private void InsertData()
        {
            ProductPriceEntity priceE =new ProductPriceEntity();
            ProductPriceService priceS = ProductPriceService.GetInstance();
            priceE.ApplyDate = dfApplyDate.SelectedDate;
            priceE.ProductId = Int32.Parse(txtProductID.Text);
            priceE.Price = Convert.ToDecimal(txtPrice.Text);
            priceE.CreatedDate = DateTime.Now;
            priceE.CreatedBy = FSSContext.Current.User.EmployeeId;
            priceE.UpdatedBy = FSSContext.Current.User.EmployeeId;
            priceE.UpdatedDate = DateTime.Now;
            List<ProductPriceEntity> listPrice = (List<ProductPriceEntity>)priceS.LoadByDate(priceE);
            if (listPrice.Count != 0)
            {
                X.Msg.Alert("Chú ý", "Đã đặt giá cho sản phẩm này ngày: " + dfApplyDate.SelectedDate.ToShortDateString()).Show();
            }
            else
            {
                if (txtPriceId.Text.Equals(""))
                {
                    priceS.Insert(priceE);
                }
                else
                {
                    priceE.ProductPriceId = Format.ConvertToInt(txtPriceId.Text);
                    priceS.Update(priceE);
                }
            }
        }
        private bool CheckInputData()
        {
            return true;
        }
    }
}